//
//  RBLuckyDrawHistoryVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/29.
//

#import "RBLuckyDrawHistoryVC.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import "UIScrollView+RBAdd.h"
#import "RBSearchTagView.h"
#import "RBThemeConfig.h"
#import <Masonry.h>
#import "RBNavigationBar.h"
#import "RBTextField.h"
#import "UIButton+RBAdd.h"
#import <UIColor+YYAdd.h>
#import "RBLuckyDrawHistoryCell.h"
#import "RBLuckyDrawHistoryResponse.h"
#import "UIViewController+RBAdd.h"
#import "RBLuckyDrawHistoryResponse.h"
#import "RBRefreshHeader.h"
#import "RBRefreshFooter.h"

@interface RBLuckyDrawHistoryVC ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)UIView *headerView;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSMutableArray *dataSource;

@property(nonatomic, strong) RBLuckyDrawHistoryResponse *response;

@end

@implementation RBLuckyDrawHistoryVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - Private

- (void)setup
{
    self.title = @"抽奖记录";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
}

- (void)fillData
{
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.headerView = self.headerView;
        group.headerHeight = Zoom(40);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
        group;
    })];
    [self appendData];
    
}

- (void)appendData
{
    NSMutableArray *arrayM = ({
        RBGroupModel *group = self.dataSource.firstObject;
        (NSMutableArray *)group.cellModels;
    });
    for (RBLuckyDrawHistoryModel *model in self.response.data)
    {
        [arrayM addObject:({
            RBCellModel *cellModel = RBCellModel.new;
            cellModel.reuseCellIdentifier = NSStringFromClass(RBLuckyDrawHistoryCell.class);
            cellModel.cellHeight = Zoom(54);
            cellModel.cellData = model;
            cellModel;
        })];
    }
    [self.tableView reloadData];
    if(!arrayM.count)
    {
        [self.tableView showPlaceHolder:RB_EMPTY_TIP tapBlock:nil];
    }
}

#pragma mark - Network

- (NSString *)listPageKey
{
    return @"page";
}
- (NSDictionary *)listParams
{
    return @{
        @"user_id":[RBAccountManager getUserId],
    };
}
- (NSString *)listRequestPath
{
    return @"hu_jiang_log";
}
- (NSInteger)ListCurrentPage
{
    return self.response.currentPage;
}
- (BOOL)listLog
{
    return YES;
}
- (BOOL)listPost
{
    return YES;
}
- (Class)listResponseClass
{
    return RBLuckyDrawHistoryResponse.class;
}
- (UIScrollView *)listView
{
    return self.tableView;
}

- (void)getFirstPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadFirstPageWithDesView:self.tableView showPlaceHolder:YES sucBlock:^(id data) {
        weak_self.response = data;
        [weak_self fillData];
    } failBlock:^(id data) {
        
    } reloadBlock:^{
        [weak_self getFirstPageData];
    }];
}

- (void)getNextPageData
{
    __weak typeof(self) weak_self = self;
    [self listLoadNextPageWithSucBlock:^(id data) {
        weak_self.response = data;
        [weak_self appendData];
    } failBlock:^(id data) {
        
    }];
}

#pragma mark - Event

#pragma mark - UITableViewDataSource & Delegate

kRBTableViewDataSourceAndDelegate(self.dataSource);


#pragma mark - Setter

#pragma mark - Getter

- (UITableView *)tableView
{
    if (!_tableView)
    {
        self.tableView = ({
            UITableView *view = [UITableView.alloc initWithFrame:CGRectZero style:UITableViewStylePlain];
            view.dataSource = self;
            view.delegate = self;
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            kRBIgnoreScrollViewInsetsAdjust(view);
            view.separatorStyle = UITableViewCellSeparatorStyleNone;
            view.tableFooterView = UIView.new;
            view.backgroundColor = [RBThemeConfig bgGrayColor];
            view.mj_header = [RBRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(getFirstPageData)];
            view.mj_footer = [RBRefreshFooter footerWithRefreshingTarget:self refreshingAction:@selector(getNextPageData)];
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.mas_equalTo(0);
                make.top.mas_equalTo(Zoom(15));
            }];
            view;
        });
    }
    return _tableView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}

- (UIView *)headerView
{
    if (!_headerView)
    {
        self.headerView = ({
            UIView *view = UIView.new;
            
            [view view_addView:({
                UIView *view = UIView.new;
                [view rbSetCornerRadius:Zoom(20)];
                view.backgroundColor = [UIColor colorWithHexString:@"#E0E0E0"];
                
                [view h_view_addView:[RanTool labelWithFontSize:Zoom(16) weight:0 color:[RBThemeConfig textBlackColor] align:1 text:@"时间" bgColor:UIColor.whiteColor radius:Zoom(20)] withHeight:CGFLOAT_MIN width:Zoom(175) top:0 bottom:0 left:0 right:CGFLOAT_MIN centerY:NO];
                
                [view h_view_addView:[RanTool labelWithFontSize:Zoom(16) weight:0 color:[RBThemeConfig textBlackColor] align:1 text:@"奖品"] withHeight:CGFLOAT_MIN width:CGFLOAT_MIN top:0 bottom:0 left:0 right:0 centerY:0];
                
                view;
            }) withHeight:Zoom(40) width:CGFLOAT_MIN top:0 bottom:0 left:Zoom(13) right:Zoom(13) centerX:0];
            
            view;
        });
    }
    return _headerView;
}


@end
